<template>
  <div id="changedialog">
    <commonformdialog
      :isshowDialogs="isshowDialog"
      :dialogTitle="dialogTitle"
      @closetextDialog="closetextDialog"
      @confirmbtn="confirmbtn"

    >
      <div class="innerLoding">
        <el-row>
          <el-form
            :model="formData"
            :rules="rules"
            ref="ruleForm"
            label-width="120px"
            size="small"
          >
            <el-row>
              <el-col :span="20">
                <el-form-item label="父级权限：" prop="parent_id">
                  <treeselect
                    v-model="formData.parent_id"
                    :options="prepareLineOption"
                    :normalizer="normalizer"
                    placeholder="请选择父级权限"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="标题：" prop="title">
                  <el-input
                    placeholder="请输入部门名称"
                    type="text"
                    clearable
                    v-model="formData.title"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="别名：" prop="alias_title">
                  <el-input
                    placeholder="请输入别名(别名用于查看显示)"
                    type="text"
                    clearable
                    v-model="formData.alias_title"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="权限级别：" prop="level">
                  <el-radio-group v-model="formData.level">
                   
                    <el-radio :label="0">超管</el-radio>
                     <el-radio :label="1">普通</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="规则类型：" prop="type">
                  <el-radio-group v-model="formData.type">
                    <el-radio :label="0">菜单</el-radio>
                    <el-radio :label="1">页面</el-radio>
                    <el-radio :label="2">按钮</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="后端规则：" prop="rule">
                  <el-input
                    placeholder="请输入后端规则(控制器/方法名 同请求用_request=1\2\3\... 区分)"
                    type="text"
                    clearable
                    v-model="formData.rule"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="前端路由：" prop="route">
                  <el-input
                    placeholder="请输入前端路由"
                    type="text"
                    clearable
                    v-model="formData.route"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="菜单icon：" prop="route_icon">
                  <el-input
                    placeholder="请输入菜单icon"
                    type="text"
                    clearable
                    v-model="formData.route_icon"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="快捷操作icon：" prop="quick_icon">
                  <el-input
                    placeholder="请输入快捷操作icon"
                    type="text"
                    clearable
                    v-model="formData.quick_icon"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
               <el-col :span="20">
                <el-form-item label="显示状态：" prop="is_show">
                  <el-radio-group v-model="formData.is_show">
                    <el-radio :label="1">显示</el-radio>
                    <el-radio :label="0">隐藏</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="状态：" prop="status">
                  <el-radio-group v-model="formData.status">
                    <el-radio :label="1">正常</el-radio>
                    <el-radio :label="0">禁用</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="描述：" prop="description">
                  <el-input
                    placeholder="请输入描述"
                    type="textarea"
                    :rows="3"
                    resize="none"
                    clearable
                    v-model="formData.description"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
             
            </el-row>
          </el-form>
        </el-row>
      </div>
    </commonformdialog>
  </div>
</template>

<script type="text/javascript">
//引入弹窗
import commonformdialog from "@/components/commondialog/commondialog";
import uploadimg from "@/components/uploadimg/uploadimg";
import Treeselect from "@riophae/vue-treeselect"; //下拉树组件
import { userRuleRreate, userRuleUpdate } from "@/api/userManagement";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  data() {
    return {
      showPicker: false,
      multipleopen: true,
      imgList: [],
      formData: {
        parent_id: null,
        title: "",
        alias_title: "",
        level: 0,
        type: 1,
        rule: "",
        route: "",
        route_icon: "",
        quick_icon: "",
        description: "",
        is_show: 1,
        status: 1,
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        level: [{ required: true, message: "请选择权限级别", trigger: "blur" }],
        type: [{ required: true, message: "请选择规则类型", trigger: "blur" }],
        rule: [{ required: true, message: "请输入后端规则", trigger: "blur" }],
        route: [{ required: true, message: "请输入前端路由", trigger: "blur" }],
        is_show: [
          { required: true, message: "请选择显示状态", trigger: "blur" },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "blur" }],
      },
    };
  },
  props: {
    //是否显示弹窗
    isshowDialog: {
      type: Boolean,
      default: false,
      required: true,
    },
    //表单的名字
    dialogTitle: {
      type: String,
      default: "",
      required: true,
    },
    //是编辑还是新增
    formType: {
      type: Number,
      default: 0,
    },
    prepareLineOption: {
      type: Array,
    },
    dialogdata: {
      type: Object,
    },
  },
  components: {
    commonformdialog,
    Treeselect,
  },
  methods: {
    normalizer(node) {
      return {
        id: node.id,
        label: node.title,
        children: node.children,
      };
    },
    changeparent_id(val, i) {
      console.log(val);
    },
    closetextDialog() {
      this.$emit("closedialog");
    },

    confirmbtn() {
      this.$refs["ruleForm"].validate(async (valid) => {
        if (!valid) return;
        let {
          parent_id,
          title,
          alias_title,
          level,
          type,
          rule,
          route,
          route_icon,
          description,
          is_show,
          status,
        } = this.formData;
        let params = {
          parent_id,
          title,
          alias_title,
          level,
          type,
          rule,
          route,
          route_icon,
          description,
          is_show,
          status,
        };
        console.log(params);
        let data;
        if (this.formType == 0) {
          data = await userRuleRreate(params);
        } else {
          params.id = this.formData.id;
          data = await userRuleUpdate(params);
        }
        if (data.code == "0000") {
          this.$message({
            type: "success",
            message: data.msg,
          });
          this.$emit("confirm");
        }
      });
    },

  },
  watch: {
    isshowDialog: function (val) {
      if (!val) {
        this.$refs["ruleForm"].resetFields();
      }
    },
    dialogdata: function (val) {
      this.formData = val;
      if (this.formData.parent_id == 0) {
        this.formData.parent_id = null;
      }
    },

  },
  created() {},
};
</script>

<style lang="scss" scoped>
::v-deep {
  #InputSearch {
    display: none !important;
  }
  .grid-emojis {
    grid-template-columns: repeat(10, 10%) !important;
  }
  .small {
    position: absolute;
    z-index: 9;
    left: -45px;
    top: 30px;
  }
}
</style>
